/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
#include "EPD_2in9.h"
#include "DEV_Config.h"
#include "GUI_Paint.h"
#include "queue.h"
#include "EPD_Test.h"
#include "cmd.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
uint8_t rx_buf[512];
uint32_t rx_len=0;
uint8_t tx_buf[512];
uint32_t tx_len=0;
CpuInfo cpuinfo;
/* USER CODE BEGIN PV */
uint8_t rx_status = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
   uint8_t buf[10];
	 uint32_t bufsize = 0;

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USB_DEVICE_Init();
  MX_SPI1_Init();
	DEV_Module_Init();
  EPD_2IN9_Init(EPD_2IN9_FULL);
  EPD_2IN9_Clear();
	UBYTE *BlackImage;
  UWORD Imagesize = ((EPD_2IN9_WIDTH % 8 == 0)? (EPD_2IN9_WIDTH / 8 ): (EPD_2IN9_WIDTH / 8 + 1)) * EPD_2IN9_HEIGHT;
  BlackImage = (UBYTE *)malloc(Imagesize);
	Paint_NewImage(BlackImage, EPD_2IN9_WIDTH, EPD_2IN9_HEIGHT, 270, WHITE);	
	InitDisplayMsg(BlackImage);

	CpuInfo cpuinfo;
	MemInfo meminfo;
	Storage storage;
	char ipaddr[16];
	uint8_t addr_len;
	uint8_t ack_buf[4] = {0xaa,0x05,0x01,0x55};
/*	
ipaddr
   aa 01 addr1 addr2 addr3 addr4 55
cpuinfo
   aa 02 temp use 55
meminfo 
   aa 03 useH useL totalH totalL percent 55
storage
   aa 04 ssd_useH ssd_useL ssd_totalH ssd_totalL ssd_percent disk_useH disk_useL disk_totalH disk_totalL disk_percent 55
*/  
  while (1)
  {
    /* USER CODE END WHILE */
       if(rx_status == 1){
		      switch(rx_buf[1]){
						case 0xf0: //clear all
							   ClearDisplayMsg(BlackImage);
							   break;
						case 0x1:// update ipaddr
							   for(int i=0; i < rx_len-3; i ++){
								    ipaddr[i] = rx_buf[i + 2];
								 }
								 ipaddr[rx_len-3]='\0';
								 UpdateIp(BlackImage,ipaddr,rx_len-2);
								 break;
						case 0x2:// update cpu info
							   cpuinfo.temp = rx_buf[2];
							   cpuinfo.use = rx_buf[3];
						     UpdateCpuInfo(BlackImage,&cpuinfo);
						     break;
						case 0x3:// update mem info
							   meminfo.use = rx_buf[2] <<8 | rx_buf[3];
						     meminfo.total = rx_buf[4] << 8 | rx_buf[5];
						     meminfo.use_percent = rx_buf[6];
						     UpdateMemInfo(BlackImage,&meminfo);
						     break;
						case 0x4:// update storage info
							   storage.ssd_use = rx_buf[2] << 8 | rx_buf[3];
						     storage.ssd_total = rx_buf[4] << 8 | rx_buf[5];
						     storage.ssd_percent = rx_buf[6];
						     storage.disk_use = rx_buf[7] << 8 | rx_buf[8];
						     storage.disk_total = rx_buf[9] << 8| rx_buf[10];
						     storage.disk_percent = rx_buf[11];
						     UpdateStorage(BlackImage,&storage);
						     break;
						case 0x5: //ack
							   CDC_Transmit_FS(ack_buf,4);
							   break;
						case 0x6: // update all
							   meminfo.use = rx_buf[2] <<8 | rx_buf[3];
						     meminfo.total = rx_buf[4] << 8 | rx_buf[5];
						     meminfo.use_percent = rx_buf[6];
						     cpuinfo.temp = rx_buf[7];
							   cpuinfo.use = rx_buf[8];
						     storage.ssd_use = rx_buf[9] << 8 | rx_buf[10];
						     storage.ssd_total = rx_buf[11] << 8 | rx_buf[12];
						     storage.ssd_percent = rx_buf[13];
						     storage.disk_use = rx_buf[14] << 8 | rx_buf[15];
						     storage.disk_total = rx_buf[16] << 8| rx_buf[17];
						     storage.disk_percent = rx_buf[18];
						     UpdateAllmsg(BlackImage, &cpuinfo, &meminfo, &storage);
							   
						default:
							  break;
					}
					rx_status = 0;
			 }
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
	free(BlackImage);
  BlackImage = NULL;
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 192;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
	__HAL_RCC_GPIOD_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC13 */
  GPIO_InitStruct.Pin = GPIO_PIN_13;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	 HAL_GPIO_WritePin(GPIOA, RST_Pin|DC_Pin|SPI_CS_Pin, GPIO_PIN_RESET);

  /*Configure GPIO pins : PAPin PAPin PAPin */
  GPIO_InitStruct.Pin = RST_Pin|DC_Pin|SPI_CS_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = BUSY_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(BUSY_GPIO_Port, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
